package com.guoguo.utils;

import java.util.Date;
import java.util.UUID;

/**
 * Created by li on 2017/6/6.
 */
public class UUIDUtils {

    private static String order_no_path = "system:guoguo:data:orderNo";

    /**
     * 生成UUID
     *
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "").toUpperCase();
    }


    public static String getOrderNO() {
        RedisUtils redisUtils = RedisUtils.getInstance();
        String day = DateUtils.getDateByFormat(new Date(), "yyyyMMdd");
        String redisKey = order_no_path + "_" + day;
        StringBuffer orderNo = new StringBuffer(day);

        if (redisUtils.exists(redisKey)) {
            orderNo.append(StringUtils.codeAddOne(redisUtils.getString(redisKey), 10));
        } else {
            String no = "0";
            orderNo.append(StringUtils.codeAddOne(no, 10));
            redisUtils.set(redisKey, no);
            //有效时间为一天
            redisUtils.expire(redisKey, 3600 * 24);
        }
        redisUtils.incr(redisKey);
        return orderNo.toString();
    }

}
